package com.financial.business.integral.service.yd.impl;

import com.financial.business.integral.constants.asyn.FinancialPaymentAsyncEnumConstants;
import com.financial.business.integral.dao.asyn.FinancialPaymentAsyncDao;
import com.financial.business.integral.domain.asyn.FinancialPaymentAsyncDO;
import com.financial.business.integral.service.yd.FinancialYDIntegralPaymentAsynService;
import com.financial.business.integral.service.yd.FinancialYDIntegralPaymentService;
import com.financial.business.integral.utils.HttpClientUtils;
import com.financial.business.integral.utils.JsonUtils;
import com.financial.business.integral.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Davidbieber
 * @description 金融中心积分代付异步业务接口实现类
 * @email davidbieber@163.com
 * @date 2019/12/12 10:47 AM
 */
@Service
public class FinancialYDIntegralPaymentAsynServiceImpl implements FinancialYDIntegralPaymentAsynService {

    public static final Logger logger = LoggerFactory.getLogger(FinancialYDIntegralPayAsynServiceImpl.class);

    @Autowired
    private FinancialPaymentAsyncDao financialPaymentAsyncDao;

    @Autowired
    private HttpClientUtils httpClientUtils;

    @Autowired
    private FinancialYDIntegralPaymentService financialYDIntegralPaymentService;

    @Override
    public ApiResult paymentAsyn(Map<String, Object> paramMap) {
        logger.info("FinancialYDIntegralPaymentAsynServiceImpl.paymentAsyn---接收优店积分代付异步回执参数map格式:" + paramMap.toString());
        String payAsynJson = JsonUtils.objectToJson(paramMap);
        String orderNum = "";
        /*初始化返回值值*/
        String result = FinancialPaymentAsyncEnumConstants.ENUM_SEND_FAIL;
        try {
            String balanceCode = paramMap.get("balanceCode").toString();
            String message = paramMap.get("message").toString();
            String prePayNo = paramMap.get("prePayNo").toString();
            String payMoney = paramMap.get("payMoney").toString();
            String customerUuid = paramMap.get("customerUuid").toString();
            orderNum = paramMap.get("orderNum").toString();
            /*获取平台编号*/
            String registBusNum = paramMap.get("registBusNum").toString();
            //todo 余额预支付异步信息通知优点平台回执记录
            FinancialPaymentAsyncDO financialPaymentAsyncDO = new FinancialPaymentAsyncDO();
            financialPaymentAsyncDO.setBusinessChannel(FinancialPaymentAsyncEnumConstants.ENUM_BUSINESSCHANNEL_GYINTEGRAL);
            financialPaymentAsyncDO.setGmtCreate(new Date());
            financialPaymentAsyncDO.setGmtModified(new Date());
            financialPaymentAsyncDO.setOutTradeNo(orderNum);
            financialPaymentAsyncDO.setTotalAmount(new BigDecimal(payMoney));
            financialPaymentAsyncDO.setReturnMessage(payAsynJson);
            financialPaymentAsyncDO.setIsDelete(FinancialPaymentAsyncEnumConstants.ENUM_ISDELETE_UNDEL);
            if (balanceCode.equals(FinancialPaymentAsyncEnumConstants.ENUM_GYINTEGRALCALLBANK_FAIL)) {
                financialPaymentAsyncDO.setState(FinancialPaymentAsyncEnumConstants.ENUM_STATE_SUC);
            } else {
                financialPaymentAsyncDO.setState(FinancialPaymentAsyncEnumConstants.ENUM_STATE_FAIL);
            }
            int res = financialPaymentAsyncDao.save(financialPaymentAsyncDO);
            if (res > 0) {
                /*获取请求回调地址*/
                Map<String, Object> condMap = new HashMap<>();
                condMap.put("outTradeNo", orderNum);
                condMap.put("state", FinancialPaymentAsyncEnumConstants.ENUM_STATE_SUC);
                String resqContent = financialYDIntegralPaymentService.query(condMap);
                logger.info("FinancialYDIntegralPaymentAsynServiceImpl.paymentAsyn---优店积分异步回执查询该笔订单的请求内容为!" + resqContent);
                if (resqContent != null && resqContent != "") {
                    /*查询resq_content内容中的notify_url回调地址*/
                    Map<String, Object> resqContentMap = JsonUtils.jsonToMap(resqContent);
                    String notifyUrl = (String) resqContentMap.get("notify_url");
                    logger.info("FinancialYDIntegralPaymentAsynServiceImpl.paymentAsyn---优店积分异步回执查询该笔订单的回执地址!" + notifyUrl);
                    /*通知调用中心*/
                    result = httpClientUtils.doPost(notifyUrl, paramMap);
                    logger.info("FinancialYDIntegralPaymentAsynServiceImpl.paymentAsyn---优店积分发送成功!");
                    return ApiResult.success(result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FinancialYDIntegralPaymentAsynServiceImpl.paymentAsyn---优店积分回调调用平台失败:" + e.getMessage());
        }
        return ApiResult.failure(result);
    }
}
